package com.intelligent.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.intelligent.pojo.ProductionMaterialRequisition;
import com.intelligent.service.ProductionMaterialRequisitionService;
import com.intelligent.mapper.ProductionMaterialRequisitionMapper;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import java.util.Map;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

/**
* @author lenovo
* @description 针对表【production_material_requisition(生产领料表)】的数据库操作Service实现
* @createDate 2025-09-07 20:38:29
*/
@Service
public class ProductionMaterialRequisitionServiceImpl extends ServiceImpl<ProductionMaterialRequisitionMapper, ProductionMaterialRequisition>
    implements ProductionMaterialRequisitionService{
    @Override
    public IPage<Map<String, Object>> pageWithJoin(Page<Map<String, Object>> page,
                                                   String requisitionNo,
                                                   Integer approvalStatus,
                                                   String productionWorkOrder,
                                                   String username,
                                                   String warehouseReceipt,
                                                   String startDate,
                                                   String endDate) {
        return baseMapper.selectPageWithJoin(page, requisitionNo, approvalStatus, productionWorkOrder, username, warehouseReceipt, startDate, endDate);
    }

    @Override
    public Map<String, Object> getDetailById(Long id) {
        return baseMapper.selectDetailById(id);
    }

    @Override
    public boolean addProductionMaterialRequisition(ProductionMaterialRequisition entity) {
        if (entity.getRequisitionNo() == null || entity.getRequisitionNo().trim().isEmpty()) {
            entity.setRequisitionNo(generateRequisitionNo());
        }
        return baseMapper.insertOne(entity) > 0;
    }

    @Override
    public boolean updateProductionMaterialRequisition(ProductionMaterialRequisition entity) {
        return baseMapper.updateOne(entity) > 0;
    }

    @Override
    public boolean deleteById(Long id) {
        return baseMapper.deleteOne(id) > 0;
    }

    private String generateRequisitionNo() {
        String datePart = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
        String randomPart = UUID.randomUUID().toString().replace("-", "").substring(0, 6).toUpperCase();
        return "PMR-" + datePart + "-" + randomPart;
    }
}




